<template>
	<xn-form-container title="新增过滤手机号" :width="550" :visible="visible" :destroy-on-close="true" @close="onClose">
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="所属项目" name="projectCode">
				<ProjectSelect v-model:value="formData.projectCode" />
			</a-form-item>
			<a-form-item label="姓名" name="name">
				<a-input v-model:value="formData.name" placeholder="请输入姓名" allow-clear />
			</a-form-item>
			<a-form-item label="身份证号" name="idCard">
				<a-input v-model:value="formData.idCard" placeholder="请输入身份证号" allow-clear />
			</a-form-item>
			<a-form-item label="手机号" name="phoneNumber">
				<a-input v-model:value="formData.phoneNumber" placeholder="请输入手机号" allow-clear />
			</a-form-item>
			<a-form-item label="备注" name="remark">
				<a-input v-model:value="formData.remark" placeholder="请输入备注" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button class="xn-mr8" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup>
	import { required } from '@/utils/formRules'
	import phoneFilterApi from '@/api/tjzy/phone/filter/phoneFilterApi'
	import ProjectSelect from '@/components/Tjzy/Project/index.vue'
	// 默认是关闭状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const submitLoading = ref(false)
	const formData = ref({})

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		formData.value = {}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
	}

	// 默认要校验的
	const formRules = {
		projectCode: [required('请选择项目')],
		phoneNumber: [required('请输入手机号')]
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			phoneFilterApi
				.add(formData.value)
				.then((res) => {
					onClose()
					emit('successful')
					submitLoading.value = false
				})
				.catch(() => {})
		})
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
